﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMS.cntr;
using SMS.util;
using System.Resources;
using System.Data.SqlTypes;

namespace SMS.view
{
    public partial class InputGradeForm : Form
    {
        public InputGradeForm()
        {
            InitializeComponent();
        }

        private void InputGradeForm_Load(object sender, EventArgs e)
        {
            bindCmbxClass();
            bindCmbxCourse();
            bindCmbxSemester();
            bindGridViewStudent();
            changeLang(I18nUtil.vnLang);
        }

        private void bindCmbxClass()
        {
            cmbxClass.DataSource = ClazzCntr.Instance.listAll();
            cmbxClass.DisplayMember = "cla_name";
            cmbxClass.ValueMember = "cla_id";
        }

        private void bindCmbxCourse()
        {
            cmbxCourse.DataSource = CourseCntr.Instance.findAll();
            cmbxCourse.DisplayMember = "cor_name";
            cmbxCourse.ValueMember = "cor_id";
        }

        private void bindCmbxSemester()
        {
            cmbxSemester.DataSource = SemesterCntr.Instance.findAll();
            cmbxSemester.DisplayMember = "sem_name";
            cmbxSemester.ValueMember = "sem_id";
        }

        private void bindGridViewStudent()
        {
            gridViewStudent.Rows.Clear();
            try
            {
                DataTable table = StudentCntr.Instance.findBy(Int32.Parse(cmbxClass.SelectedValue.ToString()), 
                                                              Int32.Parse(cmbxCourse.SelectedValue.ToString()),
                                                              Int32.Parse(cmbxSemester.SelectedValue.ToString()));

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    gridViewStudent.Rows.Add(i + 1,
                                             table.Rows[i]["STU_FULLNAME"].ToString(),
                                             table.Rows[i]["STU_EMAIL"].ToString(),
                                             table.Rows[i]["fifteen"].ToString(),
                                             table.Rows[i]["sixty"].ToString(),
                                             table.Rows[i]["final"].ToString(),
                                             table.Rows[i]["stu_id"].ToString(),
                                             table.Rows[i]["graId"].ToString());
                    gridViewStudent.Rows[gridViewStudent.RowCount - 1].Tag = table.Rows[i];
                }
                gridViewStudent.ClearSelection();
            }
            catch (Exception ex) { }
        }

        private void cmbxClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                bindGridViewStudent();
            }
            catch (Exception ex) { }
        }

        private void cmbxCourse_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                bindGridViewStudent();
            }
            catch (Exception ex) { }
        }

        private void cmbxSemester_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                bindGridViewStudent();
            }
            catch (Exception ex) { }
        }

        public void changeLang(string lang)
        {
            I18nUtil.init(lang, new ResourceManager(I18nUtil.resourcePath, typeof(AddBlockForm).Assembly));

            this.Text = I18nUtil.getString("inputGrade");
            lblClass.Text = I18nUtil.getString("lblClass");
            lblCourse.Text = I18nUtil.getString("lblCourse");
            lblSemester.Text = I18nUtil.getString("lblSemester");
            groupStudent.Text = I18nUtil.getString("student");
            gridViewStudent.Columns[0].HeaderText = I18nUtil.getString("no");
            gridViewStudent.Columns[1].HeaderText = I18nUtil.getString("fullName");
            gridViewStudent.Columns[2].HeaderText = I18nUtil.getString("email");
            gridViewStudent.Columns[3].HeaderText = I18nUtil.getString("fifteenMinTest");
            gridViewStudent.Columns[4].HeaderText = I18nUtil.getString("sixtyMinTest");
            gridViewStudent.Columns[5].HeaderText = I18nUtil.getString("finalTest");
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            int classId = Int32.Parse(cmbxClass.SelectedValue.ToString());
            int courseId = Int32.Parse(cmbxCourse.SelectedValue.ToString());
            int semesterId = Int32.Parse(cmbxSemester.SelectedValue.ToString());

            for (int i = 0; i < gridViewStudent.Rows.Count; i++)
            {
                int stuId = Int32.Parse(gridViewStudent.Rows[i].Cells[6].Value.ToString());
                SqlDouble fifteenMinTest = gridViewStudent.Rows[i].Cells[3].Value.ToString().Equals("")
                                          ? SqlDouble.Null
                                          : SqlDouble.Parse(gridViewStudent.Rows[i].Cells[3].Value.ToString());
                SqlDouble sixtyMinTest = gridViewStudent.Rows[i].Cells[4].Value.ToString().Equals("")
                                          ? SqlDouble.Null
                                          : SqlDouble.Parse(gridViewStudent.Rows[i].Cells[4].Value.ToString());
                SqlDouble finalTest = gridViewStudent.Rows[i].Cells[5].Value.ToString().Equals("")
                                          ? SqlDouble.Null
                                          : SqlDouble.Parse(gridViewStudent.Rows[i].Cells[5].Value.ToString());

                try
                {
                    if (gridViewStudent.Rows[i].Cells[7].Value.ToString().Equals(""))
                    {
                        int result = GradeCntr.Instance.insert(stuId, classId, courseId, semesterId, fifteenMinTest, sixtyMinTest, finalTest);
                    }
                    else
                    {
                        int result = GradeCntr.Instance.edit(
                            Int32.Parse(gridViewStudent.Rows[i].Cells[7].Value.ToString()), fifteenMinTest, sixtyMinTest, finalTest);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Cập nhật không thành công");
                }
            }

            MessageBox.Show("Cập nhật thành công");
            bindGridViewStudent();
        }
    }
}
